package com.cloud.security.service.impl;

import com.cloud.common.domain.Result;
import com.cloud.ding_talk.DingGrantUtils;
import com.cloud.ding_talk.DingTalkUtils;
import com.cloud.entity.AuthBody;
import com.cloud.entity.AuthUser;
import com.cloud.entity.SysProjectAuth;
import com.cloud.enums.AuthMode;
import com.cloud.enums.AuthType;
import com.cloud.exception.LoginException;
import com.cloud.security.service.ISDKClientService;
import com.cloud.system.service.ISysProjectAuthService;
import com.cloud.utils.StringUtils;
import com.cloud.wechat.WechatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 第三方认证操作
 *
 * @author yzj
 */
@Service
public class SDKClientServiceImpl implements ISDKClientService {

    @Autowired
    private ISysProjectAuthService sysProjectAuthService;

    /**
     * 根据密钥获取浙政钉用户信息
     */
    @Override
    public Result<AuthUser> getDingGrantAuthInfo(AuthBody authBody) {
        AuthUser authUser = null;
        // TODO 根据授权类型发送对应第三方账户授权,浙政钉(h5授权,APP扫码)
        if (AuthType.METHOD_ZHEZHENG_NAIL.getCode() == authBody.getAuthType()) {
            SysProjectAuth projectAuth = new SysProjectAuth();
            projectAuth.setSysCode(authBody.getSysCode());
            if (AuthMode.TALK_APP.getCode() == authBody.getAuthMode()) {
                projectAuth.setAuthType(AuthMode.TALK_APP.getCode());
                // 查询项目配置第三方授权密钥信息
                projectAuth = sysProjectAuthService.selectApplyProjectAuth(projectAuth);
                if (StringUtils.isNotNull(projectAuth)) {
                    DingGrantUtils dingTalk = new DingGrantUtils(projectAuth.getDomainName(),
                            projectAuth.getAccessKey(), projectAuth.getSecretKey());
                    // 根据APP授权Code获取用户授权信息
                    authUser = dingTalk.getDingTalkUserInfo(authBody.getAuthCode());
                    authUser.setSysCode(projectAuth.getSysCode());
                    authUser.setAuthDeptId(projectAuth.getAuthDeptId());
                } else {
                    return Result.message(LoginException.SYS_AUTH_NOT_FIND.getCode(),
                            LoginException.SYS_AUTH_NOT_FIND.getMsg());
                }
            } else if (AuthMode.TALK_PC.getCode() == authBody.getAuthMode()) {
                projectAuth.setAuthType(AuthMode.TALK_PC.getCode());
                projectAuth = sysProjectAuthService.selectApplyProjectAuth(projectAuth);
                if (StringUtils.isNotNull(projectAuth)) {
                    DingGrantUtils dingTalk = new DingGrantUtils(projectAuth.getDomainName(),
                            projectAuth.getAccessKey(), projectAuth.getSecretKey());
                    // 根据PC端扫码Code获取用户授权信息
                    authUser = dingTalk.scanDingTalkUserInfo(authBody.getAuthCode());
                    authUser.setSysCode(projectAuth.getSysCode());
                    authUser.setAuthDeptId(projectAuth.getAuthDeptId());
                } else {
                    return Result.message(LoginException.SYS_AUTH_NOT_FIND.getCode(),
                            LoginException.SYS_AUTH_NOT_FIND.getMsg());
                }
            }
        }
        return Result.success("操作成功", authUser);
    }

    /**
     * 根据密钥获取微信用户信息
     *
     * @return
     */
    @Override
    public Result<AuthUser> getWechatAuthInfo(AuthBody authBody) {
        AuthUser authUser = null;
        if (AuthType.METHOD_WECHAT.getCode() == authBody.getAuthType()) {
            SysProjectAuth projectAuth = new SysProjectAuth();
            projectAuth.setSysCode(authBody.getSysCode());
            if (AuthMode.TALK_APP.getCode() == authBody.getAuthMode()) {
                projectAuth.setAuthType(AuthMode.TALK_APP.getCode());
                // 查询项目配置第三方授权密钥信息
                projectAuth = sysProjectAuthService.selectApplyProjectAuth(projectAuth);
                if (StringUtils.isNotNull(projectAuth)) {
                    WechatUtils wechat = new WechatUtils(projectAuth.getAccessKey(),
                            projectAuth.getSecretKey());
                    // 根据H5授权Code获取用户授权信息
                    authUser = wechat.getWechatUserInfo(authBody.getAuthCode());
                } else {
                    return Result.message(LoginException.SYS_AUTH_NOT_FIND.getCode(),
                            LoginException.SYS_AUTH_NOT_FIND.getMsg());
                }
            }
        }
        return Result.success("操作成功", authUser);
    }

    /**
     * 获取微信小程序授权信息
     */
    @Override
    public Result<AuthUser> getWechatMinAppInfo(AuthBody authBody) {
        AuthUser authUser = null;
        if (AuthType.METHOD_WECHAT.getCode() == authBody.getAuthType()) {
            SysProjectAuth projectAuth = new SysProjectAuth();
            projectAuth.setSysCode(authBody.getSysCode());
            if (AuthMode.TALK_MIN_APP.getCode() == authBody.getAuthMode()) {
                projectAuth.setAuthType(AuthMode.TALK_MIN_APP.getCode());
                // 查询项目配置第三方授权密钥信息
                projectAuth = sysProjectAuthService.selectApplyProjectAuth(projectAuth);
                if (StringUtils.isNotNull(projectAuth)) {
                    // 获取微信授权信息
                    WechatUtils wechat = new WechatUtils(projectAuth.getAccessKey(),
                            projectAuth.getSecretKey());
                    authUser = wechat.getMinAppUserInfo(authBody);
                } else {
                    return Result.message(LoginException.SYS_AUTH_NOT_FIND.getCode(),
                            LoginException.SYS_AUTH_NOT_FIND.getMsg());
                }
            }
        }
        return Result.success("操作成功", authUser);
    }

    /**
     * 根据密钥获取钉钉用户信息
     */
    @Override
    public Result<AuthUser> getDingTalkAuthInfo(AuthBody authBody) {
        AuthUser authUser = null;
        if (AuthType.METHOD_DING_DING.getCode() == authBody.getAuthType()) {
            SysProjectAuth projectAuth = new SysProjectAuth();
            projectAuth.setSysCode(authBody.getSysCode());
            if (AuthMode.TALK_APP.getCode() == authBody.getAuthMode()) {
                projectAuth.setAuthType(AuthMode.TALK_APP.getCode());
                // 查询项目配置第三方授权密钥信息
                projectAuth = sysProjectAuthService.selectApplyProjectAuth(projectAuth);
                if (StringUtils.isNotNull(projectAuth)) {
                    // 获取微信授权信息
                    DingTalkUtils dingTalk = new DingTalkUtils(projectAuth.getAccessKey(),
                            projectAuth.getSecretKey());
                    authUser = dingTalk.getDingUserInfo(authBody);
                } else {
                    return Result.message(LoginException.SYS_AUTH_NOT_FIND.getCode(),
                            LoginException.SYS_AUTH_NOT_FIND.getMsg());
                }
            }
        }
        return Result.success("操作成功", authUser);
    }

}